package classes
{
	import mx.formatters.DateBase;
	import classes.Account;
	import classes.TreasuryModel;
	import classes.Transaction;
	
	public class Budget implements WritableObject
	{
		public var category:String;
		public var amount:Number = 0.00;
		public var amountRemaining:Number;
		
			
		public function Budget(){
			
		}
		
		public function parseBudgetString(raw:XML):void{
			this.category = raw.@category;
			this.amount = raw.@amount;
			this.amountRemaining = raw.@amountRemaining;
		}
		public function toWritableString():String{
			var s:String = " ";

			s = "<Budget category=\"" + this.category + "\" amount=\"" + this.amount +
							 "\" amountRemaining=\"" + this.amountRemaining + "\" />";
			
			return s;
			
		}
		//this method goes through all the accounts and their transactions and checks if any 
		// transaction was made for that category. If there was then it tracks it.
		public function calcAmountRemaining(cat:String, amo:Number):Number{
			var amountRem:Number = amo;
			var i = 0;
			// this loop goes through every account that the user has. 
			for(; i< (TreasuryModel.getInstance().currentUser.accounts.length); i++){
				// this loop goes through every regular transaction that was made in that account.
				var currentAccount:Account = TreasuryModel.getInstance().currentUser.accounts.getItemAt(i) as Account;
				var j = 0;
				for (; j<(currentAccount.transactions.length); j++){
					// go through every transaction and check if it was made in the current month
					var currentTransaction: Transaction = currentAccount.transactions.getItemAt(j) as Transaction;
					var month: Number;
					var year: Number;
					var transactionDate:Date = new Date();
					
					month = transactionDate.getMonth();
					year = transactionDate.fullYear;
					if (currentTransaction.category.toUpperCase() == cat.toUpperCase()){
						if (currentTransaction.date.getMonth() == month && currentTransaction.date.fullYear == year){
							amountRem = amountRem + currentTransaction.amount;
						}
					}
					currentTransaction = null;
				}
					
				// this loop goes through every scheduled transaction that was made in that account
				j = 0;
				for (; j<currentAccount.scheduledTransactions.length; j++){
					// go through every transaction and check if it was made in the current month
					var currentTransaction = currentAccount.transactions.getItemAt(j) as Transaction;
					var month: Number;
					var year: Number;
					var transactionDate:Date = new Date();
					
					month = transactionDate.getMonth();
					year = transactionDate.fullYear;
					if (currentTransaction.category.toUpperCase() == cat.toUpperCase()){
						if (currentTransaction.date.getMonth() == month && currentTransaction.date.fullYear == year){
									amountRem = amountRem + currentTransaction.amount;
							
						}
					}											
					currentTransaction = null;
				}
				currentAccount = null;
			}
			return amountRem as Number;
		} 
		
	}
}